import { LOGIN_USER, LOGIN_SUCCESS, LOGIN_SECOND, LOGIN_ERROR, NO_SESSION } from "./types";
import message from "antd/lib/message"
import axios from "axios";
// axios.defaults.withCredentials = true;

//用户登陆
export const signup = params => {
  return dispatch => {
    dispatch({ type: LOGIN_USER });
    const request = axios.get(`/Tgh/data/user_login?${params}`);
    request.then(function(res) {
      if (res.data.status === "first") {
        dispatch({
          type: LOGIN_SUCCESS,
          payload: res
        });
      }else if(res.data.status === "again"){
        dispatch({
          type: LOGIN_SECOND,
          payload: res
        });
      } else {
        message.error("用户名或密码出错,请重新输入");
        dispatch({
          type: LOGIN_ERROR,
          payload: "Incorrect Email/Password Combination."
        });
      }
    });
  };
};

//检测登陆
export const checkAuth = ()=>{
  return dispatch => {
    const request = axios.get(`/Tgh/data/check_login_status`);
    request.then(function(res){
      // console.log(res);
      if(res.data.status === false){
        dispatch({
          type: NO_SESSION,
          payload: '用户未登录'
        });
      }else{
        dispatch({
          type: LOGIN_SUCCESS,
          payload: res
        });
      }
    })
  };
}

// export function register(telphone, password) {
//   const url = `/Tgh/data/register_account_data?telphone=${telphone}&password=${password}`;
//   const request = axios.get(url);
//   console.log("request", request);
//   return {
//     type: REGISTER,
//     payload: request
//   };
// }
